<?php
class Db{
	private function __construct(){

	}
	public static function facetory($dbtype,$config){
		$dbtype = ucwords($dbtype);
		$path = 'dingwork/model/db/driver/'.$dbtype.'.php';
		require_once $path;
		$db = 'Db_Driver_'.$dbtype;
		if(class_exists($db)){
			return call_user_func($db .'::getInsterface',$config);
		}else{
			throw new DbException('没有找到此类型的数据库驱动');
		}
	}
}
class ModelDb{
	private $crud = null;
	private $dbconfig = null;
	public function __construct($table,$dbinfo = ''){
		if($this->_table) $table = $this->_table;
		$config = Regsiter::getInterface();
		$table = $config->get('Db')->getprefix().$table;
		$this->dbconfig = $config->get('Config');
		$db = 'DB_'.ucwords($this->dbconfig['DB_TYPE']).'_Crud';
		if (class_exists($db))
		{
			if($dbinfo)
				$this->crud = new $db($table,$dbinfo);
			else
				$this->crud = new $db($table,$config->get('Db')->getlink());
		}else
			throw new ModelDbException('数据库模块中没有这个驱动');
	}
	public function __call($method,$args){
		if(method_exists($this->crud,$method))
			return call_user_func_array(array($this->crud,$method),$args);
		else
		{
			$exp = $this->dbconfig['DB_TYPE'].'DbException';
			throw new $exp('Crud中没有这个方法');
		}
	}
}
class DbException extends DingException{}
class ModelDbException extends DbException{}
